package com.api.shopapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.api.shopapi.entity.NursingLevel;
import com.api.shopapi.entity.NursingLevelItemRel;
import com.api.shopapi.mapper.NursingLevelMapper;
import com.api.shopapi.service.NursingLevelItemRelService;
import com.api.shopapi.service.NursingLevelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service("NursingLevelServiceImplOne")
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    @Qualifier("NursingLevelItemRelServiceImplOne")
    private NursingLevelItemRelService nursingLevelItemRelService;

    @Override
    public int insert(NursingLevel nursingLevel) {
        return nursingLevelMapper.insert(nursingLevel);
    }

    @Override
    public int updateByNursingLevelCode(NursingLevel nursingLevel) {
        return nursingLevelMapper.update(nursingLevel);
    }

    @Override
    public int deleteByNursingLevelCode(String nursingLevelCode) {
        return nursingLevelMapper.delete(nursingLevelCode);
    }

    @Override
    public PageInfo<NursingLevel> getPageInfo(int pageIndex, int pageSize, String institutionCode,String name, int status) {
        PageHelper.startPage(pageIndex, pageSize);
        List<NursingLevel> listNursingLevel = nursingLevelMapper.getPageInfo(institutionCode,name,status);
        return  new PageInfo<>(listNursingLevel);
    }

    @Override
    public NursingLevel getByNameAndInstitutionCode(String name, String institutionCode) {
        return nursingLevelMapper.getByNameAndInstitutionCode(name,institutionCode);
    }

    @Override
    public NursingLevel getByNursingLevelCode(String nursingLevelCode) {
        return nursingLevelMapper.getByNursingLevelCode(nursingLevelCode);
    }

    @Override
    public List<NursingLevel> getList(String institutionCode) {
        return nursingLevelMapper.getList(institutionCode);
    }

    @Override
    @Transactional
    public int insertLeveLAndItem(NursingLevel nursingLevel, JSONArray itemCodeArr) {
        String nursingLevelCode = nursingLevel.getNursingLevelCode();
        insertNursingLevelItemRel(nursingLevelCode,itemCodeArr);
        return nursingLevelMapper.insert(nursingLevel);
    }

    public void insertNursingLevelItemRel(String nursingLevelCode,JSONArray itemCodeArr){
        for (Object i :itemCodeArr){
            String nursingItemCode = (String) i;
            NursingLevelItemRel nursingLevelItemRel = new NursingLevelItemRel();
            nursingLevelItemRel.setNursingLevelItemRelCode(UUID.randomUUID().toString());
            nursingLevelItemRel.setNursingLevelCode(nursingLevelCode);
            nursingLevelItemRel.setNursingItemCode(nursingItemCode);
            nursingLevelItemRelService.insert(nursingLevelItemRel);
        }
    }

    @Override
    @Transactional
    public int updateByNursingLevelAndNursingItem(NursingLevel nursingLevel, JSONArray itemCodeArr) {
        String nursingLevelCode = nursingLevel.getNursingLevelCode();
        nursingLevelItemRelService.delByNursingLevelCode(nursingLevelCode);
        insertNursingLevelItemRel(nursingLevelCode,itemCodeArr);
        return nursingLevelMapper.update(nursingLevel);
    }

    @Override
    public
    PageInfo<NursingLevel> pageByGovCode(int pageIndex, int pageSize, String governmentCode){
        PageHelper.startPage(pageIndex, pageSize);
        List<NursingLevel> listNursingLevel = nursingLevelMapper.listByGovCode(governmentCode);
        return  new PageInfo<>(listNursingLevel);
    }

}
